# Maintainer: Nazar Mishturak <nazar m x at gmail dot com>

_realname=libbotan
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.8.1
pkgrel=1
pkgdesc='Crypto and TLS Library for Modern C++ (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://botan.randombit.net/'
msys2_repository_url='https://github.com/randombit/botan'
msys2_references=(
  'archlinux: botan'
  "cpe: cpe:/a:botan_project:botan"
)
license=('spdx:BSD-2-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-xz")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("https://botan.randombit.net/releases/Botan-${pkgver}.tar.xz"{,.asc}
        '002-winsock-link.patch'
        '003-no-pthread.patch')
noextract=("Botan-${pkgver}.tar.xz")
sha256sums=('b039681d4b861a2f5853746d8ba806f553e23869ed72d89edbfa3c3dbfa17e68'
            'SKIP'
            'cf9974f076d91e8f58f7d9688b7ab78cc790720923c73b293871fc7b83fd55c6'
            '080efa1f4c1d004a5055201e67b5c57b62953fcdf87e5a8313da0a2ad108c320')
validpgpkeys=('621DAF6411E1851C4CF9A2E16211EBF1EFBADFBC') # Botan Distribution Key

prepare() {
  cd "${srcdir}"
  # Tarball contains Botan-${pkgver}.tgz/.travis.yml symlink
  bsdtar -xJf "Botan-${pkgver}.tar.xz" || true
  cd "Botan-${pkgver}"

  patch -p1 -i "${srcdir}/002-winsock-link.patch"
  # https://github.com/randombit/botan/pull/3934#issuecomment-2864372252
  patch -p1 -i "${srcdir}/003-no-pthread.patch"
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -rf "${srcdir}"/Botan-${pkgver} "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  local BUILD_TYPE_FLAGS=''
  if check_option 'debug' 'y'; then
    BUILD_TYPE_FLAGS='--with-debug-info'
  fi

  # See https://github.com/randombit/botan/issues/2582 for an explanation
  # on why --disable-modules=thread_utils is here.
  MSYS2_ARG_CONV_EXCL="--prefix=;--libdir=;--bindir=" \
  ${MINGW_PREFIX}/bin/python configure.py \
    --build-tool=ninja \
    --disable-modules=thread_utils \
    --os=mingw \
    --cc=${CC} \
    --prefix=${MINGW_PREFIX} \
    --cpu=${CARCH} \
    ${BUILD_TYPE_FLAGS} \
    --link-method=copy \
    --with-boost \
    --with-bzip2 \
    --with-lzma \
    --with-sqlite3 \
    --with-zlib

  ninja
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ./botan-test
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--libdir=;--bindir=" \
  DESTDIR="${pkgdir}" \
  ninja install

  MSYS2_ARG_CONV_EXCL="-p" python -m compileall \
    -o 0 -o 1 -q -s"${pkgdir}" -p"/" "${pkgdir}${MINGW_PREFIX}/lib/python"*
}
